package com.result.demo.global.advice;


import com.result.demo.global.enums.ResultCode;
import com.result.demo.global.exception.BizException;
import com.result.demo.global.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.MessageFormat;


/**
 * @author chao.z
 */
@Slf4j
@RestControllerAdvice
public class ExceptionResultAdvice  {
    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = BizException.class)
    public Result bizExceptionHandler(BizException bizException){
        log.info("进入异常处理:{}",bizException);
        return Result.failure(bizException.getResultCode());
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public  Result HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception){
        log.info("系统异常处理:{}",exception);
        String errorMsg =  MessageFormat.format(ResultCode.INVALID_REQUEST_ERROR.getMessage(), exception.getMethod(), exception.getSupportedHttpMethods());

        return Result.failure(ResultCode.INVALID_REQUEST_ERROR.getCode(),errorMsg);
    }
    /**HttpRequestMethodNotSupportedException
     * 处理系统异常
     */
    @ExceptionHandler(value = Exception.class)
    public  Result exceptionHandler(Exception exception){
        log.info("系统异常处理:{}",exception);
        return Result.error();
    }

}
